package com.springboot.apiexample.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * Result 前端框架交互
 */
@Data
@SuppressWarnings("unchecked")
public class R<T> implements Serializable {
    @SuppressWarnings("rawtypes")
    private static final R SUCCESS_RESULT = new R<>();

    @SuppressWarnings("rawtypes")
    private static final R ERROR_RESULT = new R<>(false, "操作失败");

    /**
     * 是否成功，默认true
     */
    private boolean success = Boolean.TRUE;
    /**
     * 提示信息，默认"操作成功"
     */
    private String msg = "操作成功";
    /**
     * 返回数据
     */
    @JsonInclude
    private T data;

    private int result;

    private R() {
    }

    private R(T data) {
        this.data = data;
    }

    private R(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    private R(int result, T data, String msg) {
        this.result = result;
        this.data = data;
        this.msg = msg;
    }

    public static <T> R<T> ok() {
        return SUCCESS_RESULT;
    }

    public static <T> R<T> ok(T data) {
        return new R<>(data);
    }

    public static <T> R<T> ok(int result, T data, String msg) {
        return new R<>(result, data, msg);
    }

    public static <T> R<T> failed() {
        return ERROR_RESULT;
    }

    public static <T> R<T> failed(String message) {
        return new R<>(false, message);
    }

}
